/**
 * SysDictAction.java
 * Created at 2017-1-23
 * Created by guhuajie
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.system.model.SysDict;
import com.broadtext.rms.system.service.ISysDictService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: SysDictAction</p>
 * <p>Description: 字典管理页面交互类</p>
 * <p>Author: guhuajie</p>
 * <p>Date: 2017-1-23</p>
 */
@Controller
@RequestMapping("/main/sysDict")
public class SysDictAction {

    /**
     * sysDictService 
     */
    @Autowired
    @Qualifier("sysDictService")
    private ISysDictService sysDictService;

    /**
     * <p>
     * Description: 查询字典
     * </p>
     * 
     * @param request 请求对象
     * @param response 返回对象
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping(value = "/getDict.mvc")
    @ResponseBody
    public void getDict(HttpServletRequest request, HttpServletResponse response) {
        try {
            PageBounds pageBounds;
            pageBounds = new PageBounds();
            Map<String, Object> map;
            String aoData;
            aoData = request.getParameter("aoData");
            JSONArray array;
            array = JSON.parseArray(aoData);
            map = new HashMap<String, Object>();
            if (array != null) {
                Iterator it;
                it = array.iterator();
                while (it.hasNext()) {
                    JSONObject obj;
                    obj = (JSONObject) it.next();
                    if(obj.get("value")!=null && !"".equals(obj.get("value")+"")){
                        map.put(obj.get("name")+"", obj.get("value"));
                    }
                }
            }
            pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
            pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit()+1);
            EntityPageBean<SysDict> pageBean = this.sysDictService.findEntityPageByQuery(map, pageBounds);
            Map dataMap=new HashMap();
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = null;
            try {
                out = response.getWriter();
                out.write(JSON.toJSONString(dataMap));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * <p>Description: 保存 或 更新字典</p>
     * @param request 请求
     * @param response 响应
     * @return String 操作结果
     */
    @RequestMapping(value = "/saveOrUpdateDict.mvc")
    @ResponseBody
    public Map<String, Object> saveOrUpdateDict(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> dataMap;
        dataMap = new HashMap<String, Object>();
        dataMap.put("mes", "success");
        try {
            String record;
            record = request.getParameter("record");
            JSONObject jsonOb;
            jsonOb = JSON.parseObject(record);
            SysDict dict;
            dict = new SysDict();
            if (jsonOb.get("pkId") != null && !"".equals(jsonOb.get("pkId"))) {
                dict = this.sysDictService.findEntityById(String.valueOf(jsonOb.get("pkId")));
                if (dict == null) {
                    dataMap.put("mes", "error");
                    return dataMap;
                }
            }
            if (jsonOb.get("parentKey") != null && !"".equals(jsonOb.get("parentKey"))) {
                dict.setParentKey(String.valueOf(jsonOb.get("parentKey")));
            }
            if (jsonOb.get("dictKey") != null && !"".equals(jsonOb.get("dictKey"))) {
                dict.setDictKey(String.valueOf(jsonOb.get("dictKey")));
            }
            if (jsonOb.get("dictValue") != null && !"".equals(jsonOb.get("dictValue"))) {
                dict.setDictValue(String.valueOf(jsonOb.get("dictValue")));
            }
            if (jsonOb.get("dictSort") != null && !"".equals(jsonOb.get("dictSort"))) {
                dict.setDictSort(String.valueOf(jsonOb.get("dictSort")));
            }
            if (jsonOb.get("openFlag") != null && !"".equals(jsonOb.get("openFlag"))) {
                dict.setOpenFlag(String.valueOf(jsonOb.get("openFlag")));
            }
            dict.setOpenFlag(String.valueOf(1));
            if (jsonOb.get("remark") != null && !"".equals(jsonOb.get("remark"))) {
                dict.setRemark(String.valueOf(jsonOb.get("remark")));
            }
            if (jsonOb.get("remark") != null && !"".equals(jsonOb.get("remark"))) {
                dict.setRemark(String.valueOf(jsonOb.get("remark")));
            }
            if (dict.getPkId() != null) {
                dict.setDelFlag("0");
                dict.setUpdateDate(new Date());
                dict.setUpdateUser(String.valueOf(request.getSession().getAttribute("sys.userName")));
                dataMap.put("msg", this.sysDictService.updateDict(dict));
            } else {
            	dict.setPkId(UUIDUtils.getUUID());
                dict.setDelFlag("0");
                dict.setCreateDate(new Date());
                dict.setCreateUser(String.valueOf(request.getSession().getAttribute("sys.userName")));
                dataMap.put("msg", this.sysDictService.insertDict(dict));
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return dataMap;

    }
    /**
     * 
     * <p>Description: 字典详情删除</p>
     * @param request 请求
     * @param pkId 主键id
     */
    @RequestMapping(value = "/delDict.mvc")
    @ResponseBody
    public Object delDict(HttpServletRequest request, String pkId) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        try {
            String[] idArray;
            idArray = pkId.split(",");
            String updateUser;
            updateUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
            this.sysDictService.deleteDictDetail(idArray, updateUser);
            map.put("msg", "SUCCESS");
        } catch (Exception e) {
            map.put("msg", "FAILURE");
            e.printStackTrace();
        }
        return map;

    }
}
